/***************************************************************************
 *   Copyright (C) 2007 by Eduardo Robles Elvira   *
 *   edulix@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QDebug>
#include "konntrackitem.h"

using namespace std;

QHash<QString, QString> KonntrackItem::lookUpTable;
uint KonntrackItem::count = 0;

KonntrackItem::KonntrackItem()
{
	this->data[NUMBER] = QString::number(count++);
}

KonntrackItem::~KonntrackItem()
{
	disconnect(this, 0, 0, 0);
	
}

void KonntrackItem::setData(KonntrackCol col, QString data)
{
	this->data[col] = data;
	
	if(col == ORIG_SRC_IP || col == ORIG_DST_IP || col == REPLY_SRC_IP || col == REPLY_DST_IP) {
		if(!lookUpTable.contains(data))
		{
			lookupCol = col;
			QHostInfo::lookupHost(data, this, SLOT(lookedUp(QHostInfo)));
		}
	}
}

void KonntrackItem::lookedUp(QHostInfo hi)
{
	if(!lookUpTable.contains(this->data[lookupCol]))
	{
		lookUpTable[this->data[lookupCol]] = hi.hostName();
		emit ipNameResolved();
	}
}

QString& KonntrackItem::getData(int col, bool translated)
{
	if((col == ORIG_SRC_IP || col == ORIG_DST_IP || col == REPLY_SRC_IP || col == REPLY_DST_IP) && lookUpTable.contains(data[col])) {
		if(!translated)
			return data[col];
		else
			return lookUpTable[data[col]];
	} else {
		return data[col];
	}
}

nf_conntrack* KonntrackItem::getConntrack()
{
	return ct;
}

void KonntrackItem::setConntrack(nf_conntrack *ct)
{
	this->ct = ct;
}

int KonntrackItem::removeConntrack(struct nfct_handle *h)
{
	return nfct_query(h, NFCT_Q_DESTROY, ct);
}


